package com.concerneyes.pay;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.health.Task.GlassManDataImpl;
import com.concerneyes.health.Task.GlassManDataListener;
import com.concerneyes.health.adapter.HealthAdapter;
import com.concerneyes.health.model.HealthDataModel;
import com.concerneyes.mine.TabActivity.MyAdressActivity;
import com.concerneyes.mine.Task.OrderImpl;
import com.concerneyes.mine.Task.OrderListener;
import com.concerneyes.mine.model.MyAdressModel;
import com.concerneyes.mine.model.MyGlassManModel;
import com.concerneyes.mine.model.OrderSubmit;

import java.util.List;

import common.AbsActivity;
import util.ActivityUtil;
import view.ListViewForScrollView;

/**
 * 支付后确认信息页面
 */

public class ConfirmInfoActivity extends AbsActivity implements View.OnClickListener,
        HealthAdapter.OnHealthItemClick, GlassManDataListener, OrderListener {
    private static final int CHOOSE_ADRESS = 1;
    private static final int CHOOSE_GLASS_MAN = 2;
    private static final int FAPIAO = 3;
    private ImageView mIvGone;
    private FrameLayout mFlEditInfo;
    private RelativeLayout mRlGlassPerson;
    private TextView mName;
    private TextView mSex;
    private TextView mBirthday;
    private ListViewForScrollView mLvOptometry;
    private HealthAdapter adapter;
    private TextView mOptometryDate;
    private TextView mDataCome;
    private LinearLayout mLlOptometrySingle;
    private RelativeLayout rl_order_title;
    /**
     * 收货人信息
     */
    private RelativeLayout mRlConsigneeInfo;
    /**
     * 配送方式
     */
    /**
     * 快递费
     */
    private TextView mCourierFees;
    private RelativeLayout mRlSendStyle;
    /**
     * 买家留言
     */
    private EditText mEtName;
    private RelativeLayout mRlLeaveMsg;
    /**
     * 配镜人
     */
    private TextView mCompleteOrder;
    private View glassManInfo;
    private LinearLayout viewAdress;
    private TextView tvChoseAdress;
    private TextView consigneeInfo;

    private TextView tv_name, tv_phone, tv_adress, glass_man_name;
    private GlassManDataImpl impl;
    private String orderId;

    private OrderImpl orderImpl;

    private RelativeLayout rl_fa_piao;
    private TextView fapiao;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_confirm_info);
        setTitle(R.string.confirm_msg);
        orderId = getIntent().getExtras().getString("orderId");
        initView();
    }

    private void initView() {
        mIvGone = (ImageView) findViewById(R.id.iv_gone);
        mFlEditInfo = (FrameLayout) findViewById(R.id.fl_edit_info);
        mRlGlassPerson = (RelativeLayout) findViewById(R.id.rl_glass_person);
        glass_man_name = (TextView) findViewById(R.id.glass_man_name);
        mName = (TextView) findViewById(R.id.name);
        mSex = (TextView) findViewById(R.id.sex);
        mBirthday = (TextView) findViewById(R.id.birthday);
        mLvOptometry = (ListViewForScrollView) findViewById(R.id.lv_optometry);
        mOptometryDate = (TextView) findViewById(R.id.optometry_date);
        mDataCome = (TextView) findViewById(R.id.data_come);
        mLlOptometrySingle = (LinearLayout) findViewById(R.id.ll_optometry_single);
        rl_order_title = (RelativeLayout) findViewById(R.id.rl_order_title);
        mRlConsigneeInfo = (RelativeLayout) findViewById(R.id.rl_consignee_info);
        mCourierFees = (TextView) findViewById(R.id.courier_fees);
        mRlSendStyle = (RelativeLayout) findViewById(R.id.rl_send_style);
        mEtName = (EditText) findViewById(R.id.et_name);
        mRlLeaveMsg = (RelativeLayout) findViewById(R.id.rl_leave_msg);
        mCompleteOrder = (TextView) findViewById(R.id.complete_order);
        tvChoseAdress = (TextView) findViewById(R.id.tv_chose_adress);
        consigneeInfo = (TextView) findViewById(R.id.consignee_info);


        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_adress = (TextView) findViewById(R.id.tv_adress);

        glassManInfo = findViewById(R.id.glass_man_info);
        viewAdress = (LinearLayout) findViewById(R.id.view_adress);
        rl_fa_piao = (RelativeLayout) findViewById(R.id.rl_fa_piao);
        fapiao = (TextView) findViewById(R.id.fapiao);
        rl_fa_piao.setOnClickListener(this);
        mIvGone.setOnClickListener(this);
        mRlGlassPerson.setOnClickListener(this);
        mRlConsigneeInfo.setOnClickListener(this);
        mCompleteOrder.setOnClickListener(this);
        adapter = new HealthAdapter(this, this, false, true);
        mLvOptometry.setAdapter(adapter);

        rl_order_title.setVisibility(View.GONE);
        mLlOptometrySingle.setBackground(getResources().getDrawable(R.drawable.btn_gray_bold_bg));
        impl = new GlassManDataImpl(this, this);
        orderImpl = new OrderImpl(this);
    }

    private String manId = null;
    private String dataId = null;
    private String addressId = null;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_gone:
                mFlEditInfo.setVisibility(View.GONE);
                break;
            case R.id.rl_glass_person:
                ActivityUtil.setIntentForResult(this, ChooseGlassesManActivity.class, null, CHOOSE_GLASS_MAN);
                break;
            case R.id.rl_consignee_info:
                Bundle bundle = new Bundle();
                bundle.putString("title", getResources().getString(R.string.chose_adress));
                ActivityUtil.setIntentForResult(this, MyAdressActivity.class, bundle, CHOOSE_ADRESS);
                break;
            case R.id.complete_order:
                if (TextUtils.isEmpty(manId)) {
                    showToast(R.string.choose_glassMan);
                    return;
                }
                if (TextUtils.isEmpty(addressId)) {
                    showToast(R.string.choose_adressMan);
                    return;
                }
                orderImpl.completeOrder(manId, dataId, orderId,
                        addressId, mEtName.getText().toString());
                break;
            case R.id.rl_fa_piao:
                ActivityUtil.setIntentForResult(this, FaPiaoActivity.class, null, CHOOSE_GLASS_MAN);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CHOOSE_ADRESS && resultCode == 20) {
            MyAdressModel myAdressModel = (MyAdressModel) data.getExtras().getSerializable("MyAdressModel");
            tv_name.setText(myAdressModel.getName());
            tv_phone.setText(myAdressModel.getPhone1());
            tv_adress.setText(myAdressModel.getAddress());
            tvChoseAdress.setVisibility(View.GONE);
            viewAdress.setVisibility(View.VISIBLE);
            consigneeInfo.setText(R.string.get_info);
            addressId = String.valueOf(myAdressModel.getId());

        }
        if (requestCode == CHOOSE_GLASS_MAN && resultCode == 20) {
            MyGlassManModel myGlassManModel = (MyGlassManModel) data.getExtras().getSerializable("MyGlassManModel");
            mName.setText(myGlassManModel.getRealName());
            glass_man_name.setText(myGlassManModel.getRealName());
            mSex.setText(myGlassManModel.getGender());
            mBirthday.setText(myGlassManModel.getYear() + "-" + myGlassManModel.getMonth());
            glassManInfo.setVisibility(View.VISIBLE);
            impl.listGlassManData(String.valueOf(myGlassManModel.getId()));
            manId = String.valueOf(myGlassManModel.getId());
        }
        if (requestCode == FAPIAO) {

        }
    }

    @Override
    public void healthItemClick(int position, int textPosition, View v) {

    }

    @Override
    public void glassManDataListSuccess(List<HealthDataModel> model) {
        dataId = String.valueOf(model.get(0).getId());
        adapter.setHealthData(model.get(0));
    }

    @Override
    public void glassManDataListFail() {
        adapter.setHealthData(new HealthDataModel());
    }

    @Override
    public void deleteGlassManDataSuccess(List<HealthDataModel> model) {

    }

    @Override
    public void deleteGlassManDataFail() {

    }

    @Override
    public void addGlassManDataSuccess() {

    }

    @Override
    public void addGlassManDataFail() {

    }

    @Override
    public void onSubmitSuccess(OrderSubmit msg) {

    }

    @Override
    public void onSubmitFail() {

    }

    @Override
    public void oncompleteSuccess() {
        Intent intent = new Intent();
        setResult(202, intent);
        finish();
    }

    @Override
    public void oncompleteFail() {

    }

    @Override
    public void onDeliverSuccess() {

    }

    @Override
    public void onDeliverFail() {

    }
}
